home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / perl5 / CGI::Cookie.z / CGI::Cookie
Encoding:
Text File  |  2002-10-03  |  12.3 KB  |  331 lines

  1.  
  2.  
  3.  
  4. CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))                                                  CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      CGI::Cookie - Interface to Netscape Cookies
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.          use CGI qw/:standard/;
  13.          use CGI::Cookie;
  14.  
  15.          # Create new cookies and send them
  16.          $cookie1 = new CGI::Cookie(-name=>'ID',-value=>123456);
  17.          $cookie2 = new CGI::Cookie(-name=>'preferences',
  18.                                     -value=>{ font => Helvetica,
  19.                                               size => 12 }
  20.                                     );
  21.          print header(-cookie=>[$cookie1,$cookie2]);
  22.  
  23.          # fetch existing cookies
  24.          %cookies = fetch CGI::Cookie;
  25.          $id = $cookies{'ID'}->value;
  26.  
  27.          # create cookies returned from an external source
  28.          %cookies = parse CGI::Cookie($ENV{COOKIE});
  29.  
  30.  
  31. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  32.      CGI::Cookie is an interface to Netscape (HTTP/1.1) cookies, an innovation
  33.      that allows Web servers to store persistent information on the browser's
  34.      side of the connection.  Although CGI::Cookie is intended to be used in
  35.      conjunction with CGI.pm (and is in fact used by it internally), you can
  36.      use this module independently.
  37.  
  38.      For full information on cookies see
  39.  
  40.              http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt
  41.  
  42.  
  43. UUUUSSSSIIIINNNNGGGG CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee
  44.      CGI::Cookie is object oriented.  Each cookie object has a name and a
  45.      value.  The name is any scalar value.  The value is any scalar or array
  46.      value (associative arrays are also allowed).  Cookies also have several
  47.      optional attributes, including:
  48.  
  49.      1111.... eeeexxxxppppiiiirrrraaaattttiiiioooonnnn ddddaaaatttteeee
  50.          The expiration date tells the browser how long to hang on to the
  51.          cookie.  If the cookie specifies an expiration date in the future,
  52.          the browser will store the cookie information in a disk file and
  53.          return it to the server every time the user reconnects (until the
  54.          expiration date is reached).  If the cookie species an expiration
  55.          date in the past, the browser will remove the cookie from the disk
  56.          file.  If the expiration date is not specified, the cookie will
  57.          persist only until the user quits the browser.
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))                                                  CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))
  71.  
  72.  
  73.  
  74.      2222.... ddddoooommmmaaaaiiiinnnn
  75.          This is a partial or complete domain name for which the cookie is
  76.          valid.  The browser will return the cookie to any host that matches
  77.          the partial domain name.  For example, if you specify a domain name
  78.          of ".capricorn.com", then Netscape will return the cookie to Web
  79.          servers running on any of the machines "www.capricorn.com",
  80.          "ftp.capricorn.com", "feckless.capricorn.com", etc.  Domain names
  81.          must contain at least two periods to prevent attempts to match on top
  82.          level domains like ".edu".  If no domain is specified, then the
  83.          browser will only return the cookie to servers on the host the cookie
  84.          originated from.
  85.  
  86.      3333.... ppppaaaatttthhhh
  87.          If you provide a cookie path attribute, the browser will check it
  88.          against your script's URL before returning the cookie.  For example,
  89.          if you specify the path "/cgi-bin", then the cookie will be returned
  90.          to each of the scripts "/cgi-bin/tally.pl", "/cgi-bin/order.pl", and
  91.          "/cgi-bin/customer_service/complain.pl", but not to the script
  92.          "/cgi-private/site_admin.pl".  By default, path is set to "/", which
  93.          causes the cookie to be sent to any CGI script on your site.
  94.  
  95.      4444.... sssseeeeccccuuuurrrreeee ffffllllaaaagggg
  96.          If the "secure" attribute is set, the cookie will only be sent to
  97.          your script if the CGI request is occurring on a secure channel, such
  98.          as SSL.
  99.  
  100.      CCCCrrrreeeeaaaattttiiiinnnngggg NNNNeeeewwww CCCCooooooookkkkiiiieeeessss
  101.  
  102.              $c = new CGI::Cookie(-name    =>  'foo',
  103.                                   -value   =>  'bar',
  104.                                   -expires =>  '+3M',
  105.                                   -domain  =>  '.capricorn.com',
  106.                                   -path    =>  '/cgi-bin/database'
  107.                                   -secure  =>  1
  108.                                  );
  109.  
  110.      Create cookies from scratch with the nnnneeeewwww method.  The ----nnnnaaaammmmeeee and ----vvvvaaaalllluuuueeee
  111.      parameters are required.  The name must be a scalar value.  The value can
  112.      be a scalar, an array reference, or a hash reference.  (At some point in
  113.      the future cookies will support one of the Perl object serialization
  114.      protocols for full generality).
  115.  
  116.      ----eeeexxxxppppiiiirrrreeeessss accepts any of the relative or absolute date formats recognized
  117.      by CGI.pm, for example "+3M" for three months in the future.  See
  118.      CGI.pm's documentation for details.
  119.  
  120.      ----ddddoooommmmaaaaiiiinnnn points to a domain name or to a fully qualified host name.  If
  121.      not specified, the cookie will be returned only to the Web server that
  122.      created it.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))                                                  CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))
  137.  
  138.  
  139.  
  140.      ----ppppaaaatttthhhh points to a partial URL on the current server.  The cookie will be
  141.      returned to all URLs beginning with the specified path.  If not
  142.      specified, it defaults to '/', which returns the cookie to all pages at
  143.      your site.
  144.  
  145.      ----sssseeeeccccuuuurrrreeee if set to a true value instructs the browser to return the cookie
  146.      only when a cryptographic protocol is in use.
  147.  
  148.      SSSSeeeennnnddddiiiinnnngggg tttthhhheeee CCCCooooooookkkkiiiieeee ttttoooo tttthhhheeee BBBBrrrroooowwwwsssseeeerrrr
  149.  
  150.      Within a CGI script you can send a cookie to the browser by creating one
  151.      or more Set-Cookie: fields in the HTTP header.  Here is a typical
  152.      sequence:
  153.  
  154.        my $c = new CGI::Cookie(-name    =>  'foo',
  155.                                -value   =>  ['bar','baz'],
  156.                                -expires =>  '+3M');
  157.  
  158.        print "Set-Cookie: $c\n";
  159.        print "Content-Type: text/html\n\n";
  160.  
  161.      To send more than one cookie, create several Set-Cookie: fields.
  162.      Alternatively, you may concatenate the cookies together with "; " and
  163.      send them in one field.
  164.  
  165.      If you are using CGI.pm, you send cookies by providing a -cookie argument
  166.      to the _h_e_a_d_e_r() method:
  167.  
  168.        print header(-cookie=>$c);
  169.  
  170.      Mod_perl users can set cookies using the request object's _h_e_a_d_e_r__o_u_t()
  171.      method:
  172.  
  173.        $r->header_out('Set-Cookie',$c);
  174.  
  175.      Internally, Cookie overloads the "" operator to call its _a_s__s_t_r_i_n_g()
  176.      method when incorporated into the HTTP header.  _a_s__s_t_r_i_n_g() turns the
  177.      Cookie's internal representation into an RFC-compliant text
  178.      representation.  You may call _a_s__s_t_r_i_n_g() yourself if you prefer:
  179.  
  180.        print "Set-Cookie: ",$c->as_string,"\n";
  181.  
  182.  
  183.      RRRReeeeccccoooovvvveeeerrrriiiinnnngggg PPPPrrrreeeevvvviiiioooouuuussss CCCCooooooookkkkiiiieeeessss
  184.  
  185.              %cookies = fetch CGI::Cookie;
  186.  
  187.      ffffeeeettttcccchhhh returns an associative array consisting of all cookies returned by
  188.      the browser.  The keys of the array are the cookie names.  You can
  189.      iterate through the cookies this way:
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))                                                  CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))
  203.  
  204.  
  205.  
  206.              %cookies = fetch CGI::Cookie;
  207.              foreach (keys %cookies) {
  208.                 do_something($cookies{$_});
  209.              }
  210.  
  211.      In a scalar context, _f_e_t_c_h() returns a hash reference, which may be more
  212.      efficient if you are manipulating multiple cookies.
  213.  
  214.      CGI.pm uses the URL escaping methods to save and restore reserved
  215.      characters in its cookies.  If you are trying to retrieve a cookie set by
  216.      a foreign server, this escaping method may trip you up.  Use _r_a_w__f_e_t_c_h()
  217.      instead, which has the same semantics as _f_e_t_c_h(), but performs no
  218.      unescaping.
  219.  
  220.      You may also retrieve cookies that were stored in some external form
  221.      using the _p_a_r_s_e() class method:
  222.  
  223.             $COOKIES = `cat /usr/tmp/Cookie_stash`;
  224.             %cookies = parse CGI::Cookie($COOKIES);
  225.  
  226.  
  227.      MMMMaaaannnniiiippppuuuullllaaaattttiiiinnnngggg CCCCooooooookkkkiiiieeeessss
  228.  
  229.      Cookie objects have a series of accessor methods to get and set cookie
  230.      attributes.  Each accessor has a similar syntax.  Called without
  231.      arguments, the accessor returns the current value of the attribute.
  232.      Called with an argument, the accessor changes the attribute and returns
  233.      its new value.
  234.  
  235.      nnnnaaaammmmeeee(((())))
  236.          Get or set the cookie's name.  Example:
  237.  
  238.                  $name = $c->name;
  239.                  $new_name = $c->name('fred');
  240.  
  241.  
  242.      vvvvaaaalllluuuueeee(((())))
  243.          Get or set the cookie's value.  Example:
  244.  
  245.                  $value = $c->value;
  246.                  @new_value = $c->value(['a','b','c','d']);
  247.  
  248.          vvvvaaaalllluuuueeee(((()))) is context sensitive.  In an array context it will return the
  249.          current value of the cookie as an array.  In a scalar context it will
  250.          return the ffffiiiirrrrsssstttt value of a multivalued cookie.
  251.  
  252.      ddddoooommmmaaaaiiiinnnn(((())))
  253.          Get or set the cookie's domain.
  254.  
  255.      ppppaaaatttthhhh(((())))
  256.          Get or set the cookie's path.
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))                                                  CCCCGGGGIIII::::::::CCCCooooooookkkkiiiieeee((((3333))))
  269.  
  270.  
  271.  
  272.      eeeexxxxppppiiiirrrreeeessss(((())))
  273.          Get or set the cookie's expiration time.
  274.  
  275. AAAAUUUUTTTTHHHHOOOORRRR IIIINNNNFFFFOOOORRRRMMMMAAAATTTTIIIIOOOONNNN
  276.      be used and modified freely, but I do request that this copyright notice
  277.      remain attached to the file.  You may modify this module as you wish, but
  278.      if you redistribute a modified version, please attach a note listing the
  279.      modifications you have made.
  280.  
  281.      Address bug reports and comments to:  lstein@genome.wi.mit.edu
  282.  
  283. BBBBUUUUGGGGSSSS
  284.      This section intentionally left blank.
  285.  
  286. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  287.      the _C_G_I::_C_a_r_p manpage, the _C_G_I manpage
  288.  
  289.      =cut
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.